package net.stogi.dsper.ui.client.module.datasource;

import java.util.ArrayList;
import java.util.List;

import net.stogi.common.gwt.gxt.client.core.util.WidgetUtils;
import net.stogi.common.mvp.core.behavior.Behavior;
import net.stogi.common.mvp.core.behavior.BehaviorWithValue;
import net.stogi.common.mvp.core.model.KeyValues;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Composite;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;

class DataSourceListWidget extends Composite implements DataSourceListView {

	private final Button addButton = new Button();

	private final Button cancelButton = new Button();

	private final Grid<ModelData> dataSourceGrid;

	private final ListStore<ModelData> dataSourceStore = new ListStore<ModelData>();

	private final Button editButton = new Button();

	private final ContentPanel mainPanel = new ContentPanel();

	public DataSourceListWidget() {
		List<ColumnConfig> columnConfigList = new ArrayList<ColumnConfig>();

		ColumnConfig columnConfig = new ColumnConfig();
		columnConfig.setHeader("Name");
		columnConfig.setId("name");
		columnConfig.setWidth(300);
		columnConfigList.add(columnConfig);

		columnConfig = new ColumnConfig();
		columnConfig.setHeader("URL");
		columnConfig.setId("url");
		columnConfig.setWidth(300);
		columnConfigList.add(columnConfig);

		dataSourceGrid = new Grid<ModelData>(dataSourceStore, new ColumnModel(columnConfigList));

		initComposite();
		initLayout();
		initComponent(mainPanel);
	}

	@Override
	public void appear() {
		mainPanel.show();
		// mainPanel.el().fadeIn(FxConfig.NONE);
	}

	@Override
	public void block() {
		mainPanel.setEnabled(false);
	}

	@Override
	public void disappear() {
		// mainPanel.el().fadeOut(FxConfig.NONE);
		mainPanel.hide();
		doClearDataSourceList();
	}

	@Override
	public void displayDataSources(List<KeyValues<Long>> dataSourceList) {
		WidgetUtils.populateStoreWithModel(dataSourceStore, dataSourceList);
	}

	@Override
	public void setAddBehavior(final Behavior behavior) {
		addButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setCancelBehavior(final Behavior behavior) {
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setEditBehavior(final Behavior behavior) {
		editButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				behavior.behave();
			}
		});
	}

	@Override
	public void setRowSelectedBehavior(final BehaviorWithValue<KeyValues<Long>> behavior) {
		dataSourceGrid.getSelectionModel().addSelectionChangedListener(new SelectionChangedListener<ModelData>() {

			@Override
			public void selectionChanged(SelectionChangedEvent<ModelData> se) {
				ModelData selectedItem = se.getSelectedItem();
				if (selectedItem != null) {
					behavior.behave(selectedItem.<KeyValues<Long>> get("__bean"));
				}
				else {
					behavior.behave(null);
				}
			}
		});
	}

	@Override
	public void unblock() {
		mainPanel.setEnabled(true);
	}

	private void doClearDataSourceList() {
		dataSourceStore.removeAll();
	}

	private void initComposite() {
		addButton.setText("Add");
		cancelButton.setText("Cancel");
		editButton.setText("Edit");

		dataSourceGrid.getSelectionModel().setSelectionMode(SelectionMode.SINGLE);
		dataSourceGrid.setAutoExpandColumn("url");
		dataSourceGrid.setAutoHeight(true);
		dataSourceGrid.setBorders(true);
		dataSourceGrid.setHeight(100);
		dataSourceGrid.setMinColumnWidth(200);

		mainPanel.setHeading("Data Source List");
		mainPanel.setFrame(true);
		mainPanel.setVisible(false);
	}

	private void initLayout() {
		mainPanel.add(dataSourceGrid);

		mainPanel.addButton(addButton);
		mainPanel.addButton(cancelButton);
		mainPanel.addButton(editButton);
	}
}
